package main

import (
	"fmt"
	"time"
)

var (
	myMap = make(map[int]int, 10)
)

func test(n int) {
	res := 1
	for j := 1; j <= n; j++ {
		res *= j
	}

	myMap[n] = res
}

/**

  发生错误：fatal error: concurrent map writes
  查看资源竞争的情况： -race
  例如：
   go build -race channel_01_err.go
   ././channel_01

   得到的结果：
   ==================
map[150]=0
map[199]=0
map[7]=5040
map[61]=3098476543630901248
map[97]=0
==================
WARNING: DATA RACE
Read at 0x00c00011a3c0 by main goroutine:
  main.main()
      /home/yxr/go/src/study/src/main/study/channel/channel_01_err.go:38 +0x103

Previous write at 0x00c00011a3c0 by goroutine 46:
  [failed to restore the stack]
==================
.........
map[46]=1150331055211806720
map[179]=0
map[192]=0
map[200]=0
map[4]=24
map[5]=120
map[38]=4789013295250014208
map[68]=0
Found 3 data race(s)


解除方案：
  1.全变量加锁


*/
func main() {
	for i := 1; i <= 200; i++ {
		go test(i)
	}

	//休眠10s
	time.Sleep(time.Second * 10)

	for i, v := range myMap {
		fmt.Printf("map[%d]=%d \n", i, v)
	}
}
